package com.example.tests;

import static com.example.tests.GroupDataGenerator.loadGroupsFromXmlFile;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;
import java.io.File;
import java.util.Iterator;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.example.utils.SortedListOf;

public class GroupCreationTests extends TestBase{
	
	@DataProvider
	public Iterator<Object[]> groupsFromFile() {
		//return wrapGroupDataForProvider(loadGroupsFromCsvFile(new File("groups.txt"))).iterator();
		return wrapGroupDataForProvider(loadGroupsFromXmlFile(new File("groups.xml"))).iterator();
	}
	 
	//@Test(dataProvider = "randomValidGroupsGenerator")
	@Test(dataProvider = "groupsFromFile")
	public void testGroupCreationWithValidData(GroupData group) throws Exception {
	 
	  SortedListOf<GroupData> oldList = app.getModel().getGroups();    
	  app.getGroupHelper().createGroup(group);	
	  SortedListOf<GroupData> newList = app.getModel().getGroups();
	    
	  assertThat(newList, equalTo(oldList.withAdded(group)));
	  
	  if (wantToCheck()) {
		  if ("yes".equals(app.getProperty("check.db"))) {	
			  
			  //SortedListOf<GroupData> mdGroups = app.getModel().getGroups();
			  //SortedListOf<GroupData> dbGroups = app.getHibernateHelper().listGroups();
			  
			  GroupData.assertDbGroupsEqual(app.getModel().getGroups(), app.getHibernateHelper().listGroups());
			  System.out.println("DB verification passed");
		  }
		  if ("yes".equals(app.getProperty("check.ui"))) {
			  
			  SortedListOf<GroupData> mdGroups = GroupData.buildFromGroupList(app.getModel().getGroups());
			  SortedListOf<GroupData> uiGroups = app.getGroupHelper().getUIGroups();
			  
			  assertThat(mdGroups, equalTo(uiGroups));
			  System.out.println("UI verification passed");
		  }
	  }
	  
	}

}

